﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;

namespace BanHangCshap.DanhMuc.gkhtdl63
{
    public partial class frmLoaiBoSanpham: Form
    {
        public frmLoaiBoSanpham()
        {
            InitializeComponent();
        }

        BanHangCshap.Class.gkhtdl63.BLL.LoaiBoSanphamBLL xgLoaiBoSanphamBLL = new BanHangCshap.Class.gkhtdl63.BLL.LoaiBoSanphamBLL();

        private void TaiDanhsachLBSP()
        {
            gcMain.DataSource = xgLoaiBoSanphamBLL.LoaiBoSanpham_LayDanhsach();
        }

        private void frmLoaiBoSanpham_Load(object sender, EventArgs e)
        {
            TaiDanhsachLBSP();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DanhMuc.gkhtdl63.frmLoaiBoSanphamChitiet frm = new frmLoaiBoSanphamChitiet(true,new BanHangCshap.Class.gkhtdl63.DAL.Entities.LoaiBoSanpham());
            frm.ETailai += new MyEvent_LoadData(frm_ETailai);
            frm.ShowDialog();
        }

        void frm_ETailai(object sender, ELoadData e)
        {
            if (e.IsLoadData)
            {
                TaiDanhsachLBSP();
            }
        }

        private void gvMain_DoubleClick(object sender, EventArgs e)
        {
            BanHangCshap.Class.gkhtdl63.DAL.Entities.LoaiBoSanpham xlLBSP = new BanHangCshap.Class.gkhtdl63.DAL.Entities.LoaiBoSanpham();
            try
            {
                DataRow xlDataRow = gvMain.GetFocusedDataRow();
                xlLBSP.Ma = xlDataRow["Ma"].ToString();
                xlLBSP.Ten = xlDataRow["Ten"].ToString();
                xlLBSP.Mota = xlDataRow["Mota"].ToString();
                xlLBSP.Ghichu = xlDataRow["Ghichu"].ToString();
            }
            catch (Exception)
            {

            }
            DanhMuc.gkhtdl63.frmLoaiBoSanphamChitiet frm = new frmLoaiBoSanphamChitiet(false, xlLBSP);
            frm.ETailai += new MyEvent_LoadData(frm_ETailai);
            frm.ShowDialog();
        }

        private void gvMain_MouseWheel(object sender, MouseEventArgs e)
        {
            gvMain.GridMouseWheel(e);
        }

        private void btnXemSua_Click(object sender, EventArgs e)
        {
            gvMain_DoubleClick(null, null);
        }

        private void btnTailai_Click(object sender, EventArgs e)
        {
            TaiDanhsachLBSP();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                string Ma = gvMain.GetFocusedRowCellValue("Ma").ToString();
                if (xgLoaiBoSanphamBLL.LoaiBoSanpham_Xoa(Ma))
                {
                    TaiDanhsachLBSP();
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Không thể xóa\n\nDữ liệu đang được sử dụng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Chọn loại bộ sản phẩm cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
